"""
Case Type   : resource pool
Case Name   : 监控、运维、安全策略管理员用户创建资源池，合理报错
Create At   : 2022/08/29
Owner       : songjing20
Description :
    1.创建监控、运维、安全策略管理员用户
    2.监控、运维、安全策略管理员用户创建资源池
    3.环境清理
Expect      :
    1.创建成功
    2.合理报错
    3.清理成功
History     :
"""
import os
import unittest

from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant


class ResourcePool(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f"-----{os.path.basename(__file__)} start-----")
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.respool = 'resp_resource_pool_0002'
        self.user = ('resp_resource_pool_0002_1',
                     'resp_resource_pool_0002_2',
                     'resp_resource_pool_0002_3')

    def test_resource_pool(self):
        text = "-----step1:创建监控、运维、安全策略管理员用户;expect:创建成功-----"
        self.log.info(text)
        sql = f'''drop user if exists {self.user[0]}, {self.user[1]} cascade;
        drop user if exists {self.user[2]} cascade;
        create user {self.user[0]} monadmin password '{macro.COMMON_PASSWD}';
        create user {self.user[1]} opradmin password '{macro.COMMON_PASSWD}';
        create user {self.user[2]} poladmin password '{macro.COMMON_PASSWD}';'''
        sql_res = self.pri_sh.execut_db_sql(sql)
        self.log.info(sql_res)
        self.assertEqual(sql_res.count(self.constant.CREATE_ROLE_SUCCESS_MSG),
                         3, f'执行失败:{text}')

        text = "-----step2:监控、运维、安全策略管理员用户创建资源池;expect:合理报错-----"
        self.log.info(text)
        sql = f"drop resource pool if exists {self.respool};" \
              f"create resource pool {self.respool};"
        for user in self.user:
            sql_res = self.pri_sh.execut_db_sql(
                sql, sql_type=f'-U {user} -W{macro.COMMON_PASSWD}')
            self.log.info(sql_res)
            self.assertIn('ERROR:  must be system admin or vcadmin to '
                          'create resource pool', sql_res, f'执行失败:{text}')

    def tearDown(self):
        text = "-----step3:环境清理;expect:成功-----"
        self.log.info(text)
        drop_res = self.pri_sh.execut_db_sql(
            f"drop user if exists {self.user[0]}, {self.user[1]}, "
            f"{self.user[2]} cascade;")
        self.log.info(drop_res)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, drop_res,
                      f'执行失败:{text}')
        self.log.info(f"-----{os.path.basename(__file__)} end-----")
